﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	public class LayGroupValue : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			Transaction transaction2 = null;
			if (commandData == null)
			{
				throw new System.ArgumentNullException();
			}
			UIApplication application = commandData.Application;
			Document document = application.ActiveUIDocument.Document;
			Selection selection = application.ActiveUIDocument.Selection;
			List<ViewType> list = new List<ViewType>();
            list.Add((ViewType)1);
            list.Add((ViewType)2);
            list.Add((ViewType)4);
            list.Add((ViewType)117);
			if (!YJKRevitTools.ViewCheck(application.ActiveUIDocument, list))
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			Result result;
			try
			{
				transaction2 = new Transaction(document, "组合阀门界面");
				transaction2.Start();
				FrmGroupValue frmGroupValue = new FrmGroupValue(document, 0);
				int num = (int)frmGroupValue.ShowDialog();
				transaction2.Commit();
				if (num == 1)
				{
					transaction = new Transaction(document, "布置组合阀件");
					for (;;)
					{
						transaction.Start();
						string text;
						if (frmGroupValue.nLayType == 1)
						{
							text = string.Format("请选择要布置组合阀件的水管，选择点即为组合阀门插入起始点", Array.Empty<object>());
						}
						else
						{
							text = string.Format("请选择要布置组合阀件的水管，布置点根据选取点较近的管道端点计算……", Array.Empty<object>());
						}
						Reference reference = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new PipeFilter(), text);
						XYZ project = new YJKPiping<Pipe>(document.GetElement(reference) as Pipe).GetProject(reference.GlobalPoint);
						GroupValueManage groupValueManage;
						if (frmGroupValue.nLayType == 1)
						{
							groupValueManage = new GroupValueManage(document, reference, frmGroupValue.LayFamilySymbol, project);
						}
						else
						{
							groupValueManage = new GroupValueManage(document, reference, frmGroupValue.LayFamilySymbol, (double)frmGroupValue.Distance);
						}
						if (!groupValueManage.Go())
						{
							transaction.RollBack();
						}
						else
						{
							transaction.Commit();
						}
					}
				}
				else
				{
					result = Autodesk.Revit.UI.Result.Succeeded;
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				message = ex.Message;
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				if (transaction2 != null)
				{
					TransactionStatus status = transaction2.GetStatus();
					if ((int)status == 1)
					{
						transaction2.Commit();
					}
					else if ((int)status == 5)
					{
						transaction2.RollBack();
					}
				}
				if (transaction != null)
				{
					TransactionStatus status2 = transaction.GetStatus();
                    if ((int)status2 == 1)
					{
						transaction.Commit();
					}
                    else if ((int)status2 == 5)
					{
						transaction.RollBack();
					}
				}
			}
			return result;
		}
	}
}
